<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>C-Types Foreign Function Interface (numpy.ctypeslib) &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../about.html" >
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../index.html" >
    <link rel="up" title="Routines" href="routines.html" >
    <link rel="next" title="Datetime Support Functions" href="routines.datetime.html" >
    <link rel="prev" title="numpy.char.chararray.zfill" href="generated/numpy.char.chararray.zfill.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../index.html">
      <img border=0 alt="NumPy" src="../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="index.html" >NumPy Reference</a></li>
          <li class="active"><a href="routines.html" accesskey="U">Routines</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="routines.datetime.html" title="Datetime Support Functions"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="generated/numpy.char.chararray.zfill.html" title="numpy.char.chararray.zfill"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="generated/numpy.char.chararray.zfill.html"
                        title="previous chapter">numpy.char.chararray.zfill</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="routines.datetime.html"
                        title="next chapter">Datetime Support Functions</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <span class="target" id="module-numpy.ctypeslib"></span><div class="section" id="c-types-foreign-function-interface-numpy-ctypeslib">
<h1>C-Types Foreign Function Interface (<a class="reference internal" href="#module-numpy.ctypeslib" title="numpy.ctypeslib"><code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.ctypeslib</span></code></a>)<a class="headerlink" href="#c-types-foreign-function-interface-numpy-ctypeslib" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.ctypeslib.as_array">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">as_array</code><span class="sig-paren">(</span><em class="sig-param">obj</em>, <em class="sig-param">shape=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/ctypeslib.py#L505-L523"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.as_array" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a numpy array from a ctypes array or POINTER.</p>
<p>The numpy array shares the memory with the ctypes object.</p>
<p>The shape parameter must be given if converting from a ctypes POINTER.
The shape parameter is ignored if converting from a ctypes array</p>
</dd></dl>

<dl class="function">
<dt id="numpy.ctypeslib.as_ctypes">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">as_ctypes</code><span class="sig-paren">(</span><em class="sig-param">obj</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/ctypeslib.py#L526-L544"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.as_ctypes" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and return a ctypes object from a numpy array.  Actually
anything that exposes the __array_interface__ is accepted.</p>
</dd></dl>

<dl class="function">
<dt id="numpy.ctypeslib.as_ctypes_type">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">as_ctypes_type</code><span class="sig-paren">(</span><em class="sig-param">dtype</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/ctypeslib.py#L464-L502"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.as_ctypes_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a dtype into a ctypes type.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>dtype</strong><span class="classifier">dtype</span></dt><dd><p>The dtype to convert</p>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>ctype</strong></dt><dd><p>A ctype scalar, union, array, or struct</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>NotImplementedError</strong></dt><dd><p>If the conversion is not possible</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>This function does not losslessly round-trip in either direction.</p>
<p><code class="docutils literal notranslate"><span class="pre">np.dtype(as_ctypes_type(dt))</span></code> will:</p>
<blockquote>
<div><ul class="simple">
<li><p>insert padding fields</p></li>
<li><p>reorder fields to be sorted by offset</p></li>
<li><p>discard field titles</p></li>
</ul>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">as_ctypes_type(np.dtype(ctype))</span></code> will:</p>
<blockquote>
<div><ul class="simple">
<li><p>discard the class names of <a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.Structure" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.Structure</span></code></a>s and
<a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.Union" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.Union</span></code></a>s</p></li>
<li><p>convert single-element <a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.Union" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.Union</span></code></a>s into single-element
<a class="reference external" href="https://docs.python.org/dev/library/ctypes.html#ctypes.Structure" title="(in Python v3.9)"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes.Structure</span></code></a>s</p></li>
<li><p>insert padding fields</p></li>
</ul>
</div></blockquote>
</dd></dl>

<dl class="function">
<dt id="numpy.ctypeslib.ctypes_load_library">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">ctypes_load_library</code><span class="sig-paren">(</span><em class="sig-param">*args</em>, <em class="sig-param">**kwds</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/lib/utils.py#L98-L101"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.ctypes_load_library" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#numpy.ctypeslib.ctypes_load_library" title="numpy.ctypeslib.ctypes_load_library"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes_load_library</span></code></a> is deprecated, use <a class="reference internal" href="#numpy.ctypeslib.load_library" title="numpy.ctypeslib.load_library"><code class="xref py py-obj docutils literal notranslate"><span class="pre">load_library</span></code></a> instead!</p>
<p>It is possible to load a library using
&gt;&gt;&gt; lib = ctypes.cdll[&lt;full_path_name&gt;] # doctest: +SKIP</p>
<p>But there are cross-platform considerations, such as library file extensions,
plus the fact Windows will just load the first library it finds with that name.
NumPy supplies the load_library function as a convenience.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>libname</strong><span class="classifier">str</span></dt><dd><p>Name of the library, which can have ‘lib’ as a prefix,
but without an extension.</p>
</dd>
<dt><strong>loader_path</strong><span class="classifier">str</span></dt><dd><p>Where the library can be found.</p>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>ctypes.cdll[libpath]</strong><span class="classifier">library object</span></dt><dd><p>A ctypes library object</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>OSError</strong></dt><dd><p>If there is no library with the expected extension, or the
library is defective and cannot be loaded.</p>
</dd>
</dl>
</dd>
</dl>
</dd></dl>

<dl class="function">
<dt id="numpy.ctypeslib.load_library">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">load_library</code><span class="sig-paren">(</span><em class="sig-param">libname</em>, <em class="sig-param">loader_path</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/ctypeslib.py#L93-L157"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.load_library" title="Permalink to this definition">¶</a></dt>
<dd><p>It is possible to load a library using
&gt;&gt;&gt; lib = ctypes.cdll[&lt;full_path_name&gt;] # doctest: +SKIP</p>
<p>But there are cross-platform considerations, such as library file extensions,
plus the fact Windows will just load the first library it finds with that name.
NumPy supplies the load_library function as a convenience.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>libname</strong><span class="classifier">str</span></dt><dd><p>Name of the library, which can have ‘lib’ as a prefix,
but without an extension.</p>
</dd>
<dt><strong>loader_path</strong><span class="classifier">str</span></dt><dd><p>Where the library can be found.</p>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>ctypes.cdll[libpath]</strong><span class="classifier">library object</span></dt><dd><p>A ctypes library object</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>OSError</strong></dt><dd><p>If there is no library with the expected extension, or the
library is defective and cannot be loaded.</p>
</dd>
</dl>
</dd>
</dl>
</dd></dl>

<dl class="function">
<dt id="numpy.ctypeslib.ndpointer">
<code class="sig-prename descclassname">numpy.ctypeslib.</code><code class="sig-name descname">ndpointer</code><span class="sig-paren">(</span><em class="sig-param">dtype=None</em>, <em class="sig-param">ndim=None</em>, <em class="sig-param">shape=None</em>, <em class="sig-param">flags=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/ctypeslib.py#L231-L346"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.ctypeslib.ndpointer" title="Permalink to this definition">¶</a></dt>
<dd><p>Array-checking restype/argtypes.</p>
<p>An ndpointer instance is used to describe an ndarray in restypes
and argtypes specifications.  This approach is more flexible than
using, for example, <code class="docutils literal notranslate"><span class="pre">POINTER(c_double)</span></code>, since several restrictions
can be specified, which are verified upon calling the ctypes function.
These include data type, number of dimensions, shape and flags.  If a
given array does not satisfy the specified restrictions,
a <code class="docutils literal notranslate"><span class="pre">TypeError</span></code> is raised.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>dtype</strong><span class="classifier">data-type, optional</span></dt><dd><p>Array data-type.</p>
</dd>
<dt><strong>ndim</strong><span class="classifier">int, optional</span></dt><dd><p>Number of array dimensions.</p>
</dd>
<dt><strong>shape</strong><span class="classifier">tuple of ints, optional</span></dt><dd><p>Array shape.</p>
</dd>
<dt><strong>flags</strong><span class="classifier">str or tuple of str</span></dt><dd><p>Array flags; may be one or more of:</p>
<blockquote>
<div><ul class="simple">
<li><p>C_CONTIGUOUS / C / CONTIGUOUS</p></li>
<li><p>F_CONTIGUOUS / F / FORTRAN</p></li>
<li><p>OWNDATA / O</p></li>
<li><p>WRITEABLE / W</p></li>
<li><p>ALIGNED / A</p></li>
<li><p>WRITEBACKIFCOPY / X</p></li>
<li><p>UPDATEIFCOPY / U</p></li>
</ul>
</div></blockquote>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>klass</strong><span class="classifier">ndpointer type object</span></dt><dd><p>A type object, which is an <code class="docutils literal notranslate"><span class="pre">_ndtpr</span></code> instance containing
dtype, ndim, shape and flags information.</p>
</dd>
</dl>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>TypeError</strong></dt><dd><p>If a given array does not satisfy the specified restrictions.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clib</span><span class="o">.</span><span class="n">somefunc</span><span class="o">.</span><span class="n">argtypes</span> <span class="o">=</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ctypeslib</span><span class="o">.</span><span class="n">ndpointer</span><span class="p">(</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">,</span>
<span class="gp">... </span>                                                 <span class="n">ndim</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="gp">... </span>                                                 <span class="n">flags</span><span class="o">=</span><span class="s1">&#39;C_CONTIGUOUS&#39;</span><span class="p">)]</span>
<span class="gp">... </span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clib</span><span class="o">.</span><span class="n">somefunc</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float64</span><span class="p">))</span>
<span class="gp">... </span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>